<?php 
$CI =& get_instance();
$CI->load->database();
$CI->load->library("front/header_lib");
$query_pages = $CI->db->get_where('bur_staticpages',array('staticpage_link'=>'design_directory'));
foreach($query_pages->result() as $row_vals)
{
	$title = $row_vals->staticpage_title;
    $metakey_page = $row_vals->staticpage_metakey;
    $metadesc_page = $row_vals->staticpage_metadesc;  
}
$CI->header_lib->display_header_element(array("script"=>array("jquery-1.4.2","jquery.validate.pack","jquery.NobleCount.min","ajaxfileupload","si.files"),"page_title"=>$title,"metakey_page"=>$metakey_page,"metadesc_page"=>$metadesc_page));
$datas[''] = '' ;
$CI->header_lib->display_header($datas);

$CI->load->model('Directory_company_model');
$data['photo'] = 1 ;
//echo $CI->session_user_id;
$count = $CI->Directory_company_model->get_view_count($CI->session_user_id);

$data['view_count'] = $count['view_count'];

$data['lead_count'] = $count['lead_count'];

$listing_active = $CI->Directory_company_model->get_listing_status($CI->session_user_id);
?>

<?php $left_class['photo'] = 1; ?>

<script src="<?php echo base_url(); ?>js/ajaxfileupload.js"></script>
<script src="<?php echo base_url(); ?>js/si.files.js"></script>
<style type="text/css" title="text/css">
.SI-FILES-STYLIZED label.cabinet
{
    width: 100px;
    height: 28px;
    background: url(<?php echo base_url()?>image/select_news.jpg) 0 0 no-repeat;
    display: block;
    overflow: hidden;
    cursor: pointer;
}

.SI-FILES-STYLIZED label.cabinet input.file
{
    position: relative;
    height: 100%;
    width: auto;
    opacity: 0;
    -moz-opacity: 0;
    filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);
}
</style>
<script type="text/javascript">
SI.Files.stylizeAll();
$(document).ready(function(){
    var i = $("#photo_count").val();
    //var i = cnt++;
    $('#add_more').click(function(){ 
        
        if(i>8)
        return false;
        
        $('#add_image').append('<div class="labelf"><br /><strong class="purple">Image '+i+':</strong><br /><br /><br />Caption: <br />'
            + '<span class="purple">(optional)</span></div><div class="fotor" style="position: relative"><div class="select_label">'
            
            + ' <label class="cabinet">'
            + '<input name="file_'+i+'" class="file" type="file" id="file_'+i+'" onchange="return ajaxFileUpload(this);"/>'
            + '</label>'
            
            + '</div><div style="float:left; position:relative"><img id="loading_'+i+'" src="<?php echo base_url()?>image/loading_blue.gif" style="display:none;position:absolute;z-index:1000;left:20px;top:50px" /></div>'
            + '<div id="img_'+i+'"class="select_image"">'
            + '<!--<img src="<?php echo base_url()?>xhtml/img/blank3.gif" width="75" height="75">--></div>'
            + '<div class="select_textarea"><textarea name="news_caption[]"></textarea></div>'
        );
        i++;
        return false;    
    });        
});

function show_edit(idm){
    
    var edit = '#edit_' + idm;
    $(edit).slideToggle("slow"); 
    return false;   
}
// ajax upload
function ajaxFileUpload(get_this){
    var this_id = get_this.id.split('_');
    
    if(this_id[1]){
        var div_load = "#loading_" + this_id[1];     
        $(div_load)
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
            $(this).unbind("ajaxStart");
        })    
        .ajaxStop(function(){
            $(this).unbind("ajaxComplete");    
        });
            
        var img = '#img_' + this_id[1];
        $.ajaxFileUpload({
            url:'<?php echo site_url('ajax_upload/upload_directory_image/upload/photo/network')?>/<?php echo $company_detail['company_slug_name']?>/file/'+get_this.id,
            secureuri:false,       
            fileElementId: get_this.id,
            dataType: 'json',
            success: function (data, status){
                if(typeof(data.error) != 'undefined'){
                    if(data.error != ''){                    
                        $(img).html(data.error);
                    }else{                        
                        $(img).html('<img src="<?php echo base_url();?>uploads/directory/photo/thumb/'+data.msg+'" width="75" /><br />'
                        +'<input type="hidden" name="upload[]" value="'+data.msg +'"><br />'
                        +'<input type="hidden" name="upload_ori[]" value="'+data.ori +'">');
                    }
                }
            },
            error: function (data, status, e){
                $(img).html(e);
            }
        });
    
    }else{
        var this_id = get_this.id.split('-');
        var div_load = "#loading-" + this_id[1];
     
        $(div_load)
        .ajaxStart(function(){
            $(div_load).show();
        })
        .ajaxComplete(function(){
            $(div_load).hide();
            $(this).unbind("ajaxStart");
        })
        .ajaxStop(function(){
            $(this).unbind("ajaxComplete");    
        });    
        
        var img = '#img-' + this_id[1];
        $.ajaxFileUpload({            
            url:'<?php echo site_url('ajax_upload/upload_directory_image/upload/photo/network')?>/<?php echo $company_detail['company_slug_name']?>/file/'+get_this.id,
            secureuri:false,       
            fileElementId: get_this.id,
            dataType: 'json',
            success: function (data, status){
                if(typeof(data.error) != 'undefined'){
                    if(data.error != ''){                    
                        $(img).html(data.error);
                    }else{
                        $(img).html('<img src="<?php echo base_url();?>uploads/directory/photo/thumb/'+data.msg+'" width="75" /><br />'                                               
                        +'<input id="upload-' + this_id[1] +'" type="hidden" value="'+data.msg +'"><br />'
                        +'<input type="hidden" id="upload_ori-'+ this_id[1] +'" value="'+data.ori +'">');
                    }
                }
            },
            error: function (data, status, e){
                $(img).html(e);
            }
        });  
    }    
}

function update_gallery(idp){
    var temp_capt = '#upload-image-caption-' + idp;
    var temp_file = '#upload-' + idp;
    var temp_gallery = '#gallery_' + idp;
    var temp_edit = '#edit_' + idp;
    var temp_ori = '#upload_ori-' + idp;
    
    $.post('<?php echo site_url("json_request/update_directory_photo/")?>' ,{
         id : idp,
         file : $(temp_file).val(),
         caption : $(temp_capt).val(),
         ori: $(temp_ori).val()
    },function(data){  
        $(temp_gallery).html('<a class="edit f13" onclick="show_edit('+data.id+')" >edit</a>'
        +'<img src="<?php echo base_url();?>uploads/directory/photo/thumb/'+data.file+'" width="75" />'        
        +'<input type="checkbox" name="photo_id[]" value="'+data.id+'">'
        +'<p>'+data.caption+'</p>');
        $(temp_edit).slideUp();        
    }, "json");    
}
</script>

<link rel="stylesheet" type="text/css" media="all" href="<?php echo base_url();?>xhtml/css/tia.css" />
<!--body content starts-->
<div id="container_wrapper">
	<div id="body_container">
    	<div class="left_part">
        
            <?php echo $CI->header_lib->display_panel_left($data);?>
        </div>
    	
        <div class="page">
       		<div >
	<?php 
if(!empty($company_detail['company'])){
	//echo $company_detail['company'];
}else{
	//echo "Company name not defined yet";
} 
?>
<h2 class="border">your portfolio images</h2>
<?php if(!empty($company_photo)){ ?>
<?php echo form_open('directory/panel/photo')?>
<input type="hidden" value="<?php echo count($company_photo)+2; ?>" name="photo_count" id="photo_count" />
<?php foreach($company_photo as $photo){?>
<div class="fotolist">
    <div id="gallery_<?php echo $photo['id'] ?>">
        <a class="edit" onclick="show_edit('<?php echo $photo['id'] ?>')" >edit</a>
        <?php if(!empty($photo['image'])){?>
        <img src="<?php echo base_url();?>uploads/directory/photo/thumb/<?php echo $photo['image']?>" width="75" />
        <?php }else{?>
        <img src="<?php echo base_url()?>xhtml/img/blank3.gif" width="75" height="75">
        <?php } ?>
        <input type="checkbox" name="photo_id[]" value="<?php echo $photo['id']?>">
        <p><?php echo $photo['caption']?></p>
    </div>
    <div class="clear"></div>
    <!-- Edit photo start -->
    <div id="edit_<?php echo $photo['id'] ?>" style="display:none" class="addfoto">
        <hr />    
        <div class="labelf">
            <br>
            <strong class="purple">Image :</strong>
            <br><br><br>
            Caption: <br>
            <span class="purple">(optional)</span>
        </div><!-- labelf -->
        <div class="fotor" style="position: relative">         
            <!-- ie show button -->
           
                <div class="select_label">
                <label class="cabinet"> 
                    <input name="file-<?php echo $photo['id']?>" class="file" type="file" id="file-<?php echo $photo['id']?>" onchange="return ajaxFileUpload(this);"/>
                </label>
             </div>
          
            <div style="float:left; position:relative"><img id="loading-<?php echo $photo['id']?>" src="<?php echo base_url()?>image/loading_blue.gif" style="display:none;position:absolute;z-index:1000;left:20px;top:50px" />
            </div>
            <div id="img-<?php echo $photo['id']?>" class="select_image">  
                <img src="<?php echo base_url();?>uploads/directory/photo/thumb/<?php echo $photo['image']?>" width="75" />
                <input type="hidden" id="upload-<?php echo $photo['id'] ?>" value="<?php echo $photo['image'];?>">
                <input type="hidden" id="upload_ori-<?php echo $photo['id'] ?>" value="<?php echo $photo['original'];?>">             
            </div>
            <div class="select_textarea">
            <textarea id="upload-image-caption-<?php echo $photo['id'] ?>" name="news_caption[]"><?php echo $photo['caption']?></textarea></div>
            <button onclick="update_gallery('<?php echo $photo['id'] ?>');return false;">Update</button>  
        </div><!-- fotor -->    
    </div>
    <!-- Edit photo end -->    
    <div class="clear"></div>
</div><!-- fotolist -->
<hr />
<?php } // end foreach
$form_button = array(
    'type'=>'submit',
    'name'=>'submit',
    'value'=>'delete',
    'class' => 'nolabel',
    'content'=>'DELETE SELECTED',
);
echo form_button($form_button); 
echo form_close();?>
<hr>
<?php } // end if 

if(count($company_photo) < 8 ){
?>
<h4 style="border: none" class="f17">add photos to this gallery</h4>
<?php echo form_open_multipart('directory/panel/photo')?>
<div class="addfoto">
    <!--<div id="add_image"></div>-->
    <div class="labelf">
        <br>
        <strong class="purple">Image :</strong>
        <br><br><br>

        <span class="purple"> Caption: <br />(optional)</span>
    </div><!-- labelf -->
    <div class="fotor" style="position: relative">
        <!-- ie show button -->
        <div class="select_label">
            <label class="cabinet"> 
                <input name="file_1" class="file" type="file" id="file_1" onchange="return ajaxFileUpload(this);"/>
            </label>
        
        </div>
        <div style="float:left; position:relative"><img id="loading_1" src="<?php echo base_url()?>image/loading_blue.gif" style="display:none;position:absolute;z-index:1000;" /></div>
        <div id="img_1" class="select_image"> 
            <!--<img src="<?php //echo base_url()?>xhtml/img/blank3.gif" width="75" height="75">-->             
        </div>
        <div class="select_textarea"><textarea name="news_caption[]"></textarea></div>  
    </div><!-- fotor -->
<div class="clear"></div>

</div>

<div id="add_image"></div><!-- addfoto -->
<div class="addfoto" style="clear:both">
    <div class="labelf">&nbsp;</div><!-- labelf -->

    <div class="fotor"><strong style="font-size:12px;"><a id="add_more" href="">+ add another image</a></strong> 
    </div><!-- fotor --> 
    <div class="clear"></div>       
    <hr />
    <div class="button_align">
    <?php 
            $form_button = array(
                'type'=>'submit',
                'name'=>'submit',
                'value'=>'submit',
                'content'=>'Submit',
            );
            echo form_button($form_button) ?>
    </div>
</div><!-- addfoto -->
</form>
<?php }?>
</div>
 
    	</div>
     <div class="clear"></div> 
	</div>
</div>
<!--body content ends-->


<?php
$CI->load->library("front/footer_lib");
$CI->footer_lib->display_footer();
$CI->footer_lib->display_footer_element();

?>